<link rel="stylesheet" type="text/css" href="<?php echo base_url()?>public/assets/css/h5/login.css">

<div class="weui_cells weui_cells_form">
    <div class="weui_cell">
        <div class="weui_cell_bd weui_cell_primary">
            <input id="mobile" class="weui_input" type="number" pattern="[0-9]*" placeholder="请输入手机号"/>
        </div>
        <span class="getnum">获取验证码</span>
    </div>
    <div class="weui_cell">
        
        <div class="weui_cell_bd weui_cell_primary">
            <input id="sms_code" class="weui_input" type="number" placeholder="请输入验证码"/>
        </div>
        <span ><a id="prompt" href="javascript:void(0);">没收到？</a></span>
    </div>
                
</div>
<button id="submit_btn" type="button" class="weui_btn weui_btn_primary">确定</button>
<br />   
<p style="font-size:16px;">点击“确定”，即代表同意<a href="<?php echo base_url()?>service/protocol">《叮咚老师用户协议》</a></p>


<div class="weui_dialog_confirm" id="dialog_prompt" style="display:none;">
<div class="weui_mask"></div>
<div class="weui_dialog">
    <div class="weui_dialog_hd"><strong class="weui_dialog_title">获取语言验证码</strong></div>
    <div class="weui_dialog_bd">我们将以语音电话的形式告知您验证码，请留意接听语音来电。</div>
    <div class="weui_dialog_ft">
        <a href="javascript:void(0);" class="weui_btn_dialog default">取消</a>
        <a id="sms_voice_btn" href="javascript:void(0);" class="weui_btn_dialog primary">确定</a>
    </div>
</div>
</div>

<div class="weui_dialog_confirm" id="dialog_err" style="display:none;">
<div class="weui_mask"></div>
<div class="weui_dialog">
    <div class="weui_dialog_hd"><strong class="weui_dialog_title"></strong></div>
    <div class="weui_dialog_ft">
        <a href="javascript:void(0);" class="weui_btn_dialog primary">确定</a>
    </div>
</div>
</div>

<script>
var ct_login_handle = {
    login_timeout_clear: '',
    count_down: 120,
    login_code_repeat:"获取验证码",
    is_time_running: false
};

//倒计时操作
function time_down()
{
    //改变秒数
    ct_login_handle.count_down--;
    //秒数到达后处理
    if (ct_login_handle.count_down < 1) {
        //重置秒数
        ct_login_handle.count_down = 120;
        $(".getnum").text(ct_login_handle.login_code_repeat);
        ct_login_handle.is_time_running = false;
    } else {
        var txt = ct_login_handle.count_down + "s后可再次获取";
        //显示倒计时
        $(".getnum").text(txt);

        ct_login_handle.login_timeout_clear = setTimeout("time_down()", 1000);
    }
}

function check_mobile(){
	var mobile = $("#mobile").val();
	var reg = /(13|14|15|18|17)[0-9]{9}/;

	if (!reg.test(mobile))
	{
	    $("#dialog_err").find('.weui_dialog_title').text('手机号码错误');
	    $("#dialog_err").show();
	    return false;
	}

	return true;
}

$("#submit_btn").click(function(){
	var sms_code = $("#sms_code").val();
	
	if (!sms_code)
	{
	    $("#dialog_err").find('.weui_dialog_title').text('输入验证码');
	    $("#dialog_err").show();
	    return false;
	}

	if(!check_mobile()){
	    return false;
	}
	
	$.ajax({
	    type: "POST",
	    url: '<?php echo base_url()?>login',
	    data: {'mobile': $("#mobile").val(),'sms_code':sms_code},
	    dataType: 'json',
	    success: function (data) {
	        if(data.code=='E_200'){
	            location.href = '<?php echo $return_url ? $return_url : base_url();?>';
	        }else{
	        	$("#dialog_err").find('.weui_dialog_title').text(data.msg);
	        	$("#dialog_err").show();
	        }
	    }
	});
	
});

$(".getnum").click(function(){
    //倒计时操作
    if (ct_login_handle.is_time_running) {
        return false;
    }

	if(!check_mobile()){
	    return false;
	}

	$.ajax({
	    type: "POST",
	    url: '<?php echo base_url()?>api/sms/send',
	    data: {'mobile': $("#mobile").val()},
	    dataType: 'json',
	    success: function (data) {
	        if(data.code=='E_1000'){
	        	ct_login_handle.is_time_running = true;
	            time_down();
	        }else{
	        	$("#dialog_err").find('.weui_dialog_title').text(data.msg);
	        	$("#dialog_err").show();
	        }
	    }
	});
	
});


$("#sms_voice_btn").click(function(){
	if(!check_mobile()){
	    return false;
	}
	
	$.ajax({
	    type: "POST",
	    url: '<?php echo base_url()?>api/sms/send_voice',
	    data: {'mobile': $("#mobile").val()},
	    dataType: 'json',
	    success: function (data) {
	        if(data.code=='E_1000'){
	        	$("#dialog_err").find('.weui_dialog_title').text('语音验证码获取成功，请留意接听语音来电');
	        }else{
	        	$("#dialog_err").find('.weui_dialog_title').text(data.msg);
	        }

	        $("#dialog_err").show();
	    }
	});
	
});

$("#prompt").click(function(){
	$("#dialog_prompt").show();
});

$(".weui_btn_dialog").click(function(){
	$(".weui_dialog_confirm").hide();
});


</script>